import { createRouter, createWebHistory } from 'vue-router'
import LayOut from '@/LayOut'
import { isLogin } from '@/utils'

const routes = [
  {
    path: '/',
    name: 'layOut',
    component: LayOut,
    children: [
      {
        path: '/',
        redirect: '/dashBoard'
      },
      {
        path: '/dashBoard',
        name: 'dashBoard',
        component: () => import('@v/DashBoard')
      },
      {
        path: '/cateLists',
        name: 'cateLists',
        component: () => import('@v/CateLists')
      },
      {
        path: '/itemLists',
        name: 'itemLists',
        component: () => import('@v/ItemLists')
      },
      {
        path: '/itemAdd',
        name: 'itemAdd',
        component: () => import('@v/ItemAdd')
      },
      {
        path: '/itemUpdate',
        name: 'itemUpdate',
        component: () => import('@v/ItemUpdate')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@v/LoginPage')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    if (isLogin()) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
